/**
 * Created by chenyong on 2015/11/15.
 */

var express = require('express');
var request = require('request');
var router = express.Router();
var config = require('./../config');
var bigpixelsrv = require('./bigpixelsrv');
//更新为redis数据缓存方式{2015-12-23 created by wangkui}
var CachemanRedis = require('cacheman-redis');
var cache = new CachemanRedis(config.cachestore);

function getUserInfo(userid,callback)
{
    var url = "/api/users/"+userid;
    var key='User_'+userid;
    cache.get(key,function(err,value){
        if(err|| value==undefined||value==''){
            bigpixelsrv.GetData(url,function(err,data){
                if(!err)
                {
                    var userinfo = {};
                    userinfo.userid = data._id;
                    userinfo.nickname = data.nickname;
                    if(data.hasOwnProperty('wechat'))
                    {
                        userinfo.sex = data.wechat.sex;
                    }
                    if(data.avatarId && data.avatarId.length > 0)
                    {
                        userinfo.headimgurl = config.UserAvatarPath + data.avatarId + "/96";
                    }
                    cache.set(key,userinfo,90*1000,function(err){
                        if(err){console.log(err);}
                    });
                    callback(userinfo);
                }
                else
                {
                    console.log(err);
                    callback();
                }
            })
        }else{
            callback(value);
        }
    });
}

//获取附带用户信息的标注点
exports.getUserTags = function(req,res)
{
    try
    {
        var panoid=req.params.panoid;
        var skip=req.query.skip || 0;
        var limit=req.query.limit || 100;
        var gendar = req.query.gendar || '';
        var userid = req.query.userid || '';

        var url = "/api/panos/" + panoid + "/tags?skip="+skip+"&limit="+limit + "&sex="+gendar + "&userid="+userid;

        var key='Tags_'+panoid+'_'+skip+'_'+limit+'_'+gendar+'_'+userid;
        cache.get(key,function(err,value){
            if(err|| value==undefined||value==''){
                bigpixelsrv.GetData(url,function(err,data){
                    cache.set(key,data,300,function(err){
                        if(err){console.log(err);}
                    });
                    bigpixelsrv.respondData(res,err,data);
                })
            }
            else{
                bigpixelsrv.respondData(res,err,value);
            }
        });
    }
    catch(e)
    {
        bigpixelsrv.handleGeneralError(res,e);
    }
};

//返回随机标注点
exports.getRandomUserTags = function(req,res)
{
    try
    {
        var panoid=req.params.panoid;
        var count=req.query.count;
        var url = '/api/panos/' + panoid + '/tags/random?count=' + count + '&tagtype=UserTag';
        var key='RandomTags_'+panoid+'_'+count;
        cache.get(key,function(err,value){
            if(err|| value==undefined||value==''){
                bigpixelsrv.GetData(url,function(err,data){
                    cache.set(key,data,300,function(err){
                        if(err){console.log(err);}
                    });
                    bigpixelsrv.respondData(res,err,data);
                })
            }
            else{
                bigpixelsrv.respondData(res,err,value);
            }
        });
    }
    catch(e)
    {
        bigpixelsrv.handleGeneralError(res,e);
    }
}

/**
 * 根据用户Id获取用户在某个全景图中的所有标注点
 * @param req
 * @param res
 */
exports.fetchTagsbyUserId = function(req,res)
{
    try
    {
        var panoid=req.params.panoid;
        var userid = req.query.userid;

        var url = '/api/panos/' + panoid + '/tags?&tagtype=UserTag&userid='+userid;

        bigpixelsrv.GetData(url,function(err,data){
            bigpixelsrv.respondData(res,err,data);
        })
    }
    catch(e)
    {
        bigpixelsrv.handleGeneralError(res,e);
    }
};

/**
 * 创建用户标注点
 * @param req
 * @param res
 */
exports.createUserTag = function(req,res)
{
    try
    {
        var panoid=req.params.panoid;
        var body = req.body;
        getUserInfo(body.userid,function(user){
            if(user)
            {
                body.userinfo = JSON.stringify(user)
                body.title = user.nickname;
                body.tagtype = "UserTag";
                delete body.userid;

                if ( typeof body.attached == "object")
                {
                    body.attached = JSON.stringify(body.attached)
                }
                var posturl = "/api/panos/"+panoid+"/tags";
                bigpixelsrv.PostData(posturl,body, function(error, data){
                    bigpixelsrv.respondData(res,error,data);
                });
            }
            else
            {
                throw new Error('用户信息没找到');
            }
        })
    }catch(e){
        bigpixelsrv.handleGeneralError(res,e);
    }
}

exports.updateUserTag = function(req,res)
{
    var panoid = req.params.panoid;
    var tagid = req.params.id;
    var body = req.body;
    if ( typeof body.attached == "object")
    {
        body.attached = JSON.stringify(body.attached)
    }
    var puturl = "/api/panos/" + panoid + "/tags/"+tagid;
    bigpixelsrv.PutData(puturl,body,function(error,data){
       bigpixelsrv.respondData(res,error,data);
    });
}

//粉丝
exports.fans=function(req,res){
    try{
        var panoid=req.params.id;
        var skip=req.query.skip;
        var limit=req.query.limit;
        var sex=req.query.sex;
        //构建tags查询
        var  url= "/api/panos/"+panoid+"/tags?skip="+skip+"&limit="+limit+"&sex="+sex;

        bigpixelsrv.GetData(url,function(err,data){
            bigpixelsrv.respondData(res,err,data.data);
        });
    }
    catch(e){
        bigpixelsrv.handleGeneralError(res,e);
    }
};

//设置标注
exports.settags=function(req,res){
    try
    {
        var panoid=req.params.id;
        var ath=req.body.ath;
        var atv=req.body.atv;
        var fov=req.body.fov;
        var userid=req.body.userid;
        var note=req.body.note;
        var body = {
            ath:ath,
            atv:atv,
            fov:fov,
            note:note,
            userid:userid,
            tagtype:'UserTag'
        }
        bigpixelsrv.PostData('/api/panos/'+panoid+'/tags',body, function(error, data){
            bigpixelsrv.respondData(res,error,data);
        });
    }catch(e){
        bigpixelsrv.handleGeneralError(res,e);
    }
};

//获取标注点信息
exports.gettags=function(req,res){
    try{
        var panoid=req.params.id;
        var skip;
        var limit;
        skip=req.query.skip;
        limit=req.query.limit;

        var url= '/api/panos/'+panoid+'/tags?skip='+skip+'&limit='+limit;
        bigpixelsrv.GetData(url,function(err,data){
            bigpixelsrv.respondData(res,err,data);
        });

    }catch(e){
        bigpixelsrv.handleGeneralError(res,e);
    }
};

